package com.merry.mq.springBoot.controller;

import com.merry.mq.springBoot.config.DelayedQueueConfig;
import com.merry.mq.springBoot.config.PublisherConfirmConfig;
import com.merry.mq.springBoot.config.TtlQueueConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @author frx
 * @version 1.0
 * @date 2022/7/26  1:00
 * desc:消费者，消费基于插件的延迟消息
 */
@Slf4j
@Component
public class DelayQueueConsumer {

    @RabbitListener(queues = DelayedQueueConfig.DELAYED_QUEUE_NAME)
    public void receiveDelayQueue(Message message){
        String msg = new String(message.getBody());
        log.info("当前时间：{},收到延时队列的消息：{}", new Date().toString(), msg);
    }

//    @RabbitListener(queues = TtlQueueConfig.A_QUEUE)
//    public void receiveDelayQueueA(Message message){
//        String msg = new String(message.getBody());
//        log.info("AAAA 当前时间：{},收到延时队列的消息：{}", new Date().toString(), msg);
//    }
//
//    @RabbitListener(queues = TtlQueueConfig.B_QUEUE)
//    public void receiveDelayQueueB(Message message){
//        String msg = new String(message.getBody());
//        log.info("BBBB 当前时间：{},收到延时队列的消息：{}", new Date().toString(), msg);
//    }

    @RabbitListener(queues = TtlQueueConfig.D_QUEUE)
    public void receiveDelayQueueD(Message message){
        String msg = new String(message.getBody());
        log.info("DDDD 当前时间：{},收到延时队列的消息：{}", new Date().toString(), msg);
    }

    @RabbitListener(queues = PublisherConfirmConfig.QUEUE_NAME)
    public void receiveConfirmMessage(Message message){
        String msg = new String(message.getBody());
        log.info("收到的confirm.queue 消息：{}", msg);
    }


    //消费者手动应答
    @RabbitListener(queues = PublisherConfirmConfig.QUEUE_NAME)
    public void receiveConfirmMessage1(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false); //手动应答
        String msg = new String(message.getBody());
        log.info("收到的confirm.queue 消息：{}", msg);
    }
}

